package com.hardroid.workload.ui.tablet;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;

import com.hardroid.workload.R;
import com.hardroid.workload.ui.fragment.ObsvControlFragment;
import com.hardroid.workload.ui.fragment.SheetFragment;
import com.hardroid.workload.utils.IObsvFragmentReplacable;
import com.hardroid.workload.utils.Singleton;

public class SheetMultiPaneActivity  extends Activity implements IObsvFragmentReplacable{
	
	public SheetFragment sheetFragment;
	public ObsvControlFragment controlFragment;
	
	public int id;
	
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
//		setContentView(R.layout.activity_tablet_sheet);
		
		controlFragment = (ObsvControlFragment) getFragmentManager()
				.findFragmentById(R.id.fragment_control);
		sheetFragment = (SheetFragment) getFragmentManager()
				.findFragmentById(R.id.fragment_sheet);

		if (sheetFragment == null) {
			if (!Singleton.currentProject.lEmployer.isEmpty()) {
				Log.i("Sheet Acitivy", "onCreate Sheet Instance ");
				try{
					sheetFragment = SheetFragment.createInstance(0,Singleton.currentProject.lEmployer.get(0).sheet.lObsvDay.size()-1);
				}catch(Exception e){
					sheetFragment = SheetFragment.createInstance(0,0);	
				}

				// Execute a transaction, replacing any existing fragment
				// with this one inside the frame.
				Log.i("Sheet Acitivy", "ft . begin");
				FragmentTransaction ft = getFragmentManager()
						.beginTransaction();
				ft.replace(R.id.fragment_sheet, sheetFragment);
				ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
				ft.commit();
				Log.i("Sheet Acitivy", "ft . commit");
			}
		}
		if (controlFragment!=null){
			controlFragment.setListEmployer(Singleton.currentProject.lEmployer);
		}	
		
		
	}

	public void replaceFragment(int id, int did) {
		Log.i("Sheet Acitivy", "replace fragment "+sheetFragment.employerId +" with "+id);
	
		if (sheetFragment.employerId != id){
			Log.i("Sheet Acitivy", "replace fragment create instance"+id);
			sheetFragment = SheetFragment.createInstance(id, did);
	
			// Execute a transaction, replacing any existing fragment
			// with this one inside the frame.
			Log.i("Sheet Acitivy", "replace fragment fr begin "+id);
			FragmentTransaction ft = getFragmentManager().beginTransaction();
			ft.replace(R.id.fragment_sheet, sheetFragment);
			ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
			ft.commit();
			Log.i("Sheet Acitivy", "replace fragment fr end "+id);
		}else{
			Log.i("Sheet Acitivy", "not replace"+id);
		}
//		sheetFragment.init();

	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Singleton.saveProject(Singleton.currentProject);
	}
}
